/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hva.web.mvcwebapp.models;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import nl.hva.web.mvcwebapp.doa.PageDAO;

@Entity
@Table(name = "page", uniqueConstraints = {
    @UniqueConstraint(columnNames = "PAGE_NAME"),
    @UniqueConstraint(columnNames = "PAGE_PATH")})
public class Page implements java.io.Serializable {

    private Integer pageId;
    private String pagePath;
    private String pageName;
    private String pageLayout;
    private String pageColor;
    private String pageBackground;
    private Set<PageWidget> pageWidget = new HashSet<PageWidget>(0);

    public Page() {
    }

    public Page(String stockCode, String stockName) {
        this.pagePath = stockCode;
        this.pageName = stockName;
    }

    public Page(String pagePath, String pageName,
            Set<PageWidget> pageWidget) {
        this.pagePath = pagePath;
        this.pageName = pageName;
        this.pageWidget = pageWidget;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "PAGE_ID", unique = true, nullable = false)
    public Integer getPageId() {
        return this.pageId;
    }

    public void setPageId(Integer pageId) {
        this.pageId = pageId;
    }

    @Column(name = "PAGE_PATH", unique = true, nullable = false)
    public String getPagePath() {
        return this.pagePath;
    }

    public void setPagePath(String pagePath) {
        this.pagePath = pagePath;
    }

    @Column(name = "PAGE_NAME", unique = true, nullable = false)
    public String getPageName() {
        return this.pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }

    @Column(name = "PAGE_LAYOUT", nullable = false, length = 50)
    public String getPageLayout() {
        return this.pageLayout;
    }

    public void setPageLayout(String pageLayout) {
        this.pageLayout = pageLayout;
    }

    @Column(name = "PAGE_COLOR", nullable = false, length = 50)
    public String getPageColor() {
        return this.pageColor;
    }

    public void setPageColor(String pageColor) {
        this.pageColor = pageColor;
    }

    @Column(name = "PAGE_BACKGROUND", nullable = false, length = 50)
    public String getPageBackground() {
        return this.pageBackground;
    }

    public void setPageBackground(String pageBackground) {
        this.pageBackground = pageBackground;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.page", cascade = CascadeType.ALL)
    public Set<PageWidget> getPageWidget() {
        return this.pageWidget;
    }

    public void setPageWidget(Set<PageWidget> pageWidget) {
        this.pageWidget = pageWidget;
    }

    public void addWidget(Widget widget, int location) {
        PageWidget pagewidget = new PageWidget();
        pagewidget.setWidget(widget);
        pagewidget.setPage(this);
        pagewidget.setWidgetLocation(location);
        PageDAO.insertPageWidget(pagewidget);
        this.pageWidget.add(pagewidget);
    }
}